from django.shortcuts import render,HttpResponse,redirect
from .models import *
from django.core.paginator import Paginator
# Create your views here.
#作者添加#
def writeradd(request):
    if request.method=='GET':
        return render(request,'writeradd.html')
    if request.method=='POST':
        name=request.POST.get('name')
        age=request.POST.get('age')
        sex=request.POST.get('sex')
        hobby=request.POST.get('hobby')
        dynasty=request.POST.get('dynasty')
        author=Author(name=name,age=age,sex=sex,hobby=hobby,dynasty=dynasty)
        author.save()
        return redirect('index')

#查看所有诗人+分页#
def index(request):
    author=Author.objects.all()
    page=request.GET.get('page')
    paginator=Paginator(author,3)
    nowpage=paginator.get_page(page)#当前页
    lastnumber=nowpage.number-1
    nextnumber=nowpage.number+1
    maxpage=paginator.num_pages #最大页
    print(type(maxpage))
    print(maxpage)
    return render(request,'index.html',context={'author': author,'lastnumber':lastnumber,'maxpage':maxpage,
                                                'nextnumber':nextnumber, 'nowpage':nowpage})

#诗词添加#

def poemadd(request):
    if request.method=='GET':
        return render(request,'poemadd.html')
    if request.method=='POST':
        title=request.POST.get('title')
        thefirstleague=request.POST.get('thefirstleague')
        hanlian=request.POST.get('hanlian')
        gengian=request.POST.get('gengian')
        tailal=request.POST.get('tailal')
        author=request.POST.get('author')
        poem=Poem(title=title,thefirstleague=thefirstleague,hanlian=hanlian,gengian=gengian,
                  tailal=tailal,author_id=author)
        poem.save()
        return redirect('index')
#诗词展示
def showpoem(request,id):
    poemaa = Author.objects.get(id=id).author.all()
    return render(request, 'showpoem.html', context={'poemaa': poemaa})
#诗词修改
def poem_alter(request,id):
    if request.method=='GET':
        alter = Poem.objects.get(id=id)
        return render(request,'poemalter.html',context={'alter':alter})
    if request.method=='POST':
        title=request.POST.get('title')
        thefirstleague=request.POST.get('thefirstleague')
        hanlian=request.POST.get('hanlian')
        gengian=request.POST.get('gengian')
        tailal=request.POST.get('tailal')
        author=request.POST.get('author')
        alter=Poem.objects.get(id=id)
        alter.title=title
        alter.thefirstleague=thefirstleague
        alter.hanlian=hanlian
        alter.gengian=gengian
        alter.tailal=tailal
        alter.author_id=author
        alter.save()
        return redirect('index')
#删除诗词
def dele(request,id):
    poem=Poem.objects.get(id=id)
    poem.delete()
    return redirect('index')

#诗词搜索
def search(request):
    if request.method=="GET":
        return render(request,'index.html')
    if request.method=="POST":
        title=request.POST.get('title')
        searcha=Poem.objects.filter(title__contains=title)
        return render(request,'searchshow.html',{'searcha':searcha})
